package cn.kkdlk.generate.dxf;

import cn.kkdlk.generate.dxf.model.base.BaseDxfEntity;
import cn.kkdlk.generate.dxf.model.support.DxfExtent;
import cn.kkdlk.generate.dxf.util.FileUtil;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.function.Function;

public class DxfGenSupport {

    public static void appendDxf(String dxfFilePath, String generateNewDxf, Function<Integer, BaseDxfEntity> fun) throws Exception {
        try (DxfDocWriter dxfDocWriter = new DxfDocWriter(dxfFilePath, null, StandardCharsets.UTF_8)) {
            for (int i = 0; i < 50; i++) {
                BaseDxfEntity apply = fun.apply(i);
                dxfDocWriter.addEntity(apply);
            }
            FileUtil.deleteFile(new File(generateNewDxf));
            dxfDocWriter.save(generateNewDxf, true);
        }
    }

    public static <T extends BaseDxfEntity> void generateDxf(String generateNewDxf, DxfExtent extent, List<T> entity) {
        try (DxfDocWriter dxfDocWriter = new DxfDocWriter(null, extent, StandardCharsets.UTF_8)) {
            for (T t : entity) {
                dxfDocWriter.addEntity(t);
            }
            FileUtil.deleteFile(new File(generateNewDxf));
            dxfDocWriter.save(generateNewDxf, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
